package com.app.Custom;

import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;

public class ListWorkRatingAdapter extends ArrayAdapter<WorkRating>{
	ArrayList<WorkRating> array;
	int resource;
	Context context;
	public ListWorkRatingAdapter(Context context,int textViewResourceId, ArrayList<WorkRating> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.resource = textViewResourceId;
		this.array = objects;
	}

	//Phuong thuc xac dinh View ma Adapter hien thi, o day la CustomViewRating
	
	@Override
	public View getView (int position, View convertView, ViewGroup parent){
		View workView = convertView;
		//Kiem tra workView neu null thi tao moi
		if(workView == null){
			workView = new CustomViewrRating(getContext());
		}
		//Lay ve doi tuong work hien tai
		final WorkRating work = array.get(position);
		if(work != null){
			TextView txtUserName = ((CustomViewrRating)workView).txtUserName;
			final TextView txtRating = ((CustomViewrRating)workView).txtRating;
			RatingBar RatingBar = ((CustomViewrRating)workView).ratingBar;
			
			//set su kien khi thay doi RatingBar
			RatingBar.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
				
				@Override
				public void onRatingChanged(RatingBar ratingBar, float rating,
						boolean fromUser) {
					// TODO Auto-generated method stub
					final int numStars = ratingBar.getNumStars();
					Log.d("phu", String.valueOf(numStars));
					txtRating.setText(rating + "/"+ numStars);
					work.setRating(rating);
					work.setNumberstar(numStars);
				}
			});
			//Lay ve noi dung cho textView dua vao doi tuong work hien tai
			txtUserName.setText(work.getTxtusername());
			txtRating.setText(String.valueOf(work.getRating()));
		}
		return workView;
	}
}
